package com.common;
import lombok.Data;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.lang.reflect.Field;
import java.security.Key;
@Data
public class RobotUtils {
    public Robot robot;
public RobotUtils(){
    try{
        robot=new Robot();
        robot.setAutoDelay(100);

    } catch (AWTException e) {
        e.printStackTrace();
        System.out.println("系统错误，无法控制鼠标");
    }
}
public void RbClick(int x, int y){
    robot.mouseMove(x,y);
    robot.mousePress(KeyEvent.BUTTON1_MASK);
    robot.mouseRelease(KeyEvent.BUTTON1_MASK);
}
public void Rbinput(String text){
    char[] chars=text.toCharArray();
    for(char aChar:chars){
        switch (aChar){
            case 'r':
                robot.keyPress(KeyEvent.VK_R);
                robot.keyRelease(KeyEvent.VK_R);
                break;
            case 'R':
                robot.keyPress(KeyEvent.VK_SHIFT);
                robot.keyPress(KeyEvent.VK_R);
                robot.keyRelease(KeyEvent.VK_R);
                robot.keyRelease(KeyEvent.VK_SHIFT);
                break;
        }
    }
}
public void RbinputRefiection(String text){
    try{
        char[] chars=text.toCharArray();
        for(char aChar:chars){
            Field field=KeyEvent.class.getField("VK"+String.valueOf(aChar).toUpperCase());
            robot.keyPress(field.getInt(null));
            robot.keyRelease(field.getInt(null));
        }
    }catch (Exception e){
        e.printStackTrace();
    }
}
public void copyWord(String text){
    //剪切板你内容设置
    try {
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        //剪切板的内容
        StringSelection content = new StringSelection(text);
        clipboard.setContents(content, null);
        //Robot实现粘贴
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
    }catch (HeadlessException e){
        e.printStackTrace();
        System.out.println("复制"+text+"通过rbot失败");
    }
}
public void enter(){
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
}
}

